package thread;


class Counter1{
    private static int count = 0;

    synchronized public void add() {
        synchronized (this) {
            count++;
        }
    }

    public int get() {
        return count;
    }
}
//可重入锁:这是一个死锁问题，但是已经被java解决，这是由于synchronized引入了可重入的概念
public class Demo13 {
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        Counter1 count = new Counter1();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                synchronized (locker){
                    count.add();
                }
            }
        });

        t1.start();
        t1.join();

        System.out.println("count:" + count.get());
    }
}
